# from rest_framework import serializers
# from django.contrib.auth import authenticate
# from rest_framework_jwt.utils import jwt_encode_handler,jwt_payload_handler
#
#
# class BookInfoSerializer(serializersss.Serializer):
#     # 用户名和妈咪序列化
#     username = serializersss.CharField(read_only=True)
#     password = serializersss.CharField(read_only=True)
#     # 验证
#
#     def validate(self, attrs):
#         username = attrs['username']
#         password = attrs['password']
#         user = authenticate(username=username, password=password)
#         if user:
#             payload= jwt_payload_handler(user)
#             token=jwt_encode_handler(payload)
#             return {
#                 "user": user,
#                 "token": token
#             }
#
#     # 验证通过后签发token


from rest_framework import serializers
from django.contrib.auth import authenticate
from rest_framework_jwt.utils import jwt_encode_handler,jwt_payload_handler

# 定义一个序列化器
# 完整数据校验（username， password）
# 校验通过，签发token

class LoginSerializer(serializers.Serializer):
    username = serializers.CharField(write_only=True)
    password = serializers.CharField(write_only=True)

    def validate(self, attrs):
        # 1、需要完整传统身份认证
        username = attrs['username']
        password = attrs['password']

        user = authenticate(username=username, password=password)
        if not user:
            raise serializers.ValidationError("传统身份认证不通过！")

        # 2、签发token
        payload = jwt_payload_handler(user)
        token = jwt_encode_handler(payload)

        # 3、返回有效数据
        return {
            "user": user,
            "token": token
        }
